package com.idealighter.game.web.checklevel.shuihu.service;

import com.google.common.collect.Table;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.idealighter.game.dictionary.dic.ShuihuBonusNumCfgDic;
import com.idealighter.game.games.shuihu.common.RewardCalculator;
import com.idealighter.game.games.shuihu.common.ShuiHuReward;
import com.idealighter.game.games.shuihu.struct.ShuiHuSeat;
import com.idealighter.utils.check.EmptyUtil;
import com.idealighter.utils.code.RandCodeUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Data;

@Singleton
public class ShuiHuBasicControlCheck {
  // 图标行数
  public static final int ICON_ROWS = 3;
  // 图标列数
  public static final int ICON_COLUMNS = 5;
  @Inject
  private ShuihuBonusNumCfgDic shuihuBonusNumCfgDic;
  private static List<Long> betList = new ArrayList<Long>();

  static {
    betList.add(10L);
    betList.add(20L);
    betList.add(50L);
    betList.add(100L);
    betList.add(20000L);
    betList.add(50000L);
  }

  /**
   * .
   * 
   * @param basicInputs .
   * @return .
   */
  public List<ShuihuCheckResult> check(List<ShuiHuIconInput> basicInputs) {
    List<ShuiHuIconWeight> weights = null;
    List<ShuihuCheckResult> result = new ArrayList<ShuihuCheckResult>();
    for (ShuiHuIconInput item : basicInputs) {
      weights = new ArrayList<ShuiHuIconWeight>();
      ShuiHuIconWeight weight = new ShuiHuIconWeight();
      weight.setId(1);
      weight.setWeight(item.getTf());
      weights.add(weight);

      weight = new ShuiHuIconWeight();
      weight.setId(2);
      weight.setWeight(item.getYq());
      weights.add(weight);

      weight = new ShuiHuIconWeight();
      weight.setId(3);
      weight.setWeight(item.getJd());
      weights.add(weight);

      weight = new ShuiHuIconWeight();
      weight.setId(4);
      weight.setWeight(item.getLzs());
      weights.add(weight);

      weight = new ShuiHuIconWeight();
      weight.setId(5);
      weight.setWeight(item.getLc());
      weights.add(weight);

      weight = new ShuiHuIconWeight();
      weight.setId(6);
      weight.setWeight(item.getSj());
      weights.add(weight);

      weight = new ShuiHuIconWeight();
      weight.setId(7);
      weight.setWeight(item.getTtxd());
      weights.add(weight);

      weight = new ShuiHuIconWeight();
      weight.setId(8);
      weight.setWeight(item.getZyt());
      weights.add(weight);

      weight = new ShuiHuIconWeight();
      weight.setId(9);
      weight.setWeight(item.getShz());
      weights.add(weight);

      ShuiHuIconGroup group = new ShuiHuIconGroup(weights);
      ShuihuCheckResult checkResult = checkBasic(1000000, group);
      checkResult.setId(item.getId());

      result.add(checkResult);
    }

    return result;
  }

  private ShuihuCheckResult checkBasic(int betCount, ShuiHuIconGroup iconGroup) {
    ShuiHuSeat seat = new ShuiHuSeat((byte) 0, null);
    ShuihuCheckResult result = new ShuihuCheckResult();
    for (int count = 0; count < betCount; count++) {
      seat.setLine(RandCodeUtil.random(1, 9));
      seat.setBet(RandCodeUtil.randomList(betList));
      Table<Integer, Integer, Integer> icons = seat.getIcons();
      for (int i = 0; i < ICON_ROWS; i++) {
        for (int j = 0; j < ICON_COLUMNS; j++) {
          // 合计权重
          int totalWeight = iconGroup.totalWeight;
          // 随机数
          int rand = RandCodeUtil.random(totalWeight);
          int sum = 0;
          int icon = 0;

          for (ShuiHuIconWeight iconWeight : iconGroup.getList()) {
            sum += iconWeight.getWeight();
            if (rand < sum) {
              icon = iconWeight.getId();
              break;
            }
          }
          icons.put(i, j, icon);
        }
      }
      ShuiHuReward reward = RewardCalculator.calculate(seat.getIcons(),
          shuihuBonusNumCfgDic.map(), seat.getLine(), seat.getBet());

      result.betCount++;
      result.betChips += seat.getBet() * seat.getLine();
      result.bonus += reward.getBonus();
      result.winChips += reward.getWin();
      if (reward.getWin() > 0L) {
        result.winCount++;
        Long typeCount = result.typeMap.get(reward.getType());
        if (typeCount == null) {
          typeCount = 0L;
        }
        result.typeMap.put(reward.getType(), typeCount + 1);
      }
    }

    return result;
  }

  @Data
  public static class IdCount {
    private int id;
    private long count;
  }

  @Data
  public static class ShuihuCheckResultDto {
    private String id;
    private long betChips = 0L; // 下注金额
    private long winChips = 0L; // 赢的金币
    private long bonus = 0L; // 赢的bonus次数
    private List<IdCount> typeList = new ArrayList<IdCount>(); // 奖励情况次数， 0:普通连线奖励
    // ,全盘奖励[1:铁斧,2:银枪,3:金刀,4:鲁智深,5:林冲,6:宋江,7:替天行道,8:忠义堂,9:水浒传,10:人物,11:武器
    private long betCount = 0L; // 下注次数
    private long winCount = 0L; // 赢的把数
  }

  /**
   * bo转dto.
   * 
   * @Title bo2dto.
   * @author houdongsheng
   * @date 2018年1月12日 下午5:50:16
   * @param bo bo对象
   * @return dto对象
   */
  public static ShuihuCheckResultDto bo2dto(ShuihuCheckResult bo) {
    ShuihuCheckResultDto result = null;
    if (bo != null) {
      result = new ShuihuCheckResultDto();
      result.setId(bo.getId());
      result.setBetChips(bo.getBetChips());
      result.setWinChips(bo.getWinChips());
      result.setBonus(bo.getBonus());
      result.setBetCount(bo.getBetCount());
      result.setWinCount(bo.getWinCount());

      List<IdCount> list = new ArrayList<IdCount>();
      for (int id : bo.getTypeMap().keySet()) {
        Long count = bo.getTypeMap().get(id);
        IdCount item = new IdCount();
        item.setId(id);
        item.setCount(count);
        list.add(item);
      }
      result.setTypeList(list);
    }
    return result;
  }

  /**
   * bo列表转dto列表.
   * 
   * @Title bo2dto.
   * @author houdongsheng
   * @date 2018年1月12日 下午5:50:16
   * @param list bo列表
   * @return dto列表
   */
  public static List<ShuihuCheckResultDto> bo2dto(List<ShuihuCheckResult> list) {
    List<ShuihuCheckResultDto> resultList = null;
    if (EmptyUtil.listIsNotEmpty(list)) {
      resultList = new ArrayList<ShuihuCheckResultDto>();
      for (ShuihuCheckResult item : list) {
        ShuihuCheckResultDto resultItem = new ShuihuCheckResultDto();
        resultItem = bo2dto(item);
        resultList.add(resultItem);
      }
    }
    return resultList;
  }


  @Data
  public class ShuihuCheckResult {
    private String id;
    private long betChips = 0L; // 下注金额
    private long winChips = 0L; // 赢的金币
    private long bonus = 0L; // 赢的bonus次数
    private Map<Integer, Long> typeMap = new HashMap<Integer, Long>(); // 奖励情况次数， 0:普通连线奖励
    // ,全盘奖励[1:铁斧,2:银枪,3:金刀,4:鲁智深,5:林冲,6:宋江,7:替天行道,8:忠义堂,9:水浒传,10:人物,11:武器
    private long betCount = 0L; // 下注次数
    private long winCount = 0L; // 赢的把数
  }


  @Data
  public static class ShuiHuIconInput {
    private String id;
    private int tf;
    private int yq;
    private int jd;
    private int lzs;
    private int lc;
    private int sj;
    private int ttxd;
    private int zyt;
    private int shz;
  }


  public static class ShuiHuIconGroup {
    private int totalWeight = 0;
    private List<ShuiHuIconWeight> list = new ArrayList<ShuiHuIconWeight>();

    /**
     * 构造函数 .
     * 
     * @param list .
     */
    public ShuiHuIconGroup(List<ShuiHuIconWeight> list) {
      for (ShuiHuIconWeight item : list) {
        this.totalWeight += item.getWeight();
        this.list.add(item);
      }
    }

    public int getTotalWeight() {
      return totalWeight;
    }

    public List<ShuiHuIconWeight> getList() {
      return list;
    }
  }


  @Data
  public static class ShuiHuIconWeight {
    private int id;
    private int weight;
  }
}
